
# __init__.py 为初始化加载文件
import time

# 导入系统资源模块
from ascript.android.system import R
# 导入动作模块
from ascript.android import action
# 导入节点检索模块
from ascript.android import node
# 导入图色检索模块
from ascript.android import screen
from ascript.android.ui import Loger
# 获取屏幕信息
from ascript.android.system import Device
def ocr():
    # 谷歌OCR 识别中文,并使用自动分割
    from ascript.android.screen import Ocr
    res = Ocr.mlkitocr_v2()
    orc_txt = {}
    if res:
        # 循环打印识别到的每一个段落
        for r in res:
            # print("文字:", r.text)
            if "退" in r.text :
                r.text = "退"
            if "猫舍" in r.text :
                r.text = "猫舍"
            if "签到" in r.text :
                r.text = "签到"
            if "朋友圈" in r.text :
                r.text = "朋友圈"
            # print("文字中心坐标:", r.center_x, r.center_y)
            # print("文字范围:", r.rect)

            orc_txt[r.text] = (r.center_x, r.center_y)
    return orc_txt
# print(ocr())
def watch_ad_orc(wid):
    main_txt = ocr()
    print("main_txtmain_txtmain_txtmain_txtmain_txt",main_txt)
    if "签到" in main_txt:
        share_open_pos = main_txt["签到"]
        action.click(share_open_pos[0], share_open_pos[1])
        time.sleep(5)
        share_orc = ocr()
        print("share_orcshare_orcshare_orcshare_orc",share_orc)
        for i in range(10):
            if "朋友圈" not in share_orc:
                # action.click(share_ad_pos[wid][0], share_ad_pos[wid][1])
                print("=============================================")
                ad_box = node.Selector().path(
                    "/FrameLayout/FrameLayout/WebView/WebView/View/View/View/View/TextView").text("广告").parent(
                    1).find()
                if ad_box:
                    action.click(ad_box.rect.right - 10, ad_box.rect.centerY())
                    print("============click ad_box", ad_box.rect.left, ad_box.rect.right)

                time.sleep(1)
                share_orc = ocr()
                if "放弃" in share_orc:
                    time.sleep(5)
                    action.click(share_orc["放弃"][0], share_orc["放弃"][1])

        if "朋友圈" in share_orc:
            open_ad_pos = share_orc["朋友圈"]
            action.click(open_ad_pos[0], open_ad_pos[1])
            time.sleep(3)
            for i in range(10):
                print("================= i", i)
                ad_max_box = node.Selector().path("/FrameLayout/ViewGroup").find()
                if not ad_max_box:
                    temp_orc = ocr()
                    time.sleep(3)
                    if "退" in temp_orc:
                        share_close_pos = temp_orc["退"]
                        action.click(share_close_pos[0], share_close_pos[1])
                        print("=================click_ close share")
                        break
                    action.Key.back()
                ad_orc = ocr()
                if "拒绝" in ad_orc:
                    action.click(ad_orc["拒绝"][0], ad_orc["拒绝"][1])
                    print("================click refuse ad_orc")
                if "放弃" in ad_orc:
                    #跳转微信
                    time.sleep(5)
                    action.click(ad_orc["放弃"][0], ad_orc["放弃"][1])
                    print("================click refuse weixin ")
                    break
                ad_item_bos=  node.Selector().path("/FrameLayout/ViewGroup/ViewGroup/ViewGroup").find()
                if ad_item_bos:
                    ad_item_bos_brother = node.Selector().path("/FrameLayout/ViewGroup/ViewGroup/ViewGroup").brother(1).find_all()
                    print(ad_item_bos_brother)
                    if ad_item_bos_brother!=None  or ad_item_bos.rect.left<300:
                        print("find wrong node", ad_item_bos.rect.left)
                    else:
                        ad_item_bos.click()
                        print("================click close tem",ad_item_bos.rect.left,ad_item_bos.rect.right,ad_item_bos.rect.centerY())
                # for temclose in ["close1.png","close2.png"]:
                #     temclose_pos = screen.FindImages.find_template([R.img(temclose), ], confidence=0.95)
                #     if temclose_pos:
                #         print(temclose_pos)
                #         action.click(temclose_pos["result"][0], temclose_pos["result"][1])
                #         print("================close tem")
                time.sleep(5)
            action.Key.back()
            time.sleep(3)
            share_orc = ocr()
            print("=================share_orc=============",share_orc)
            if "X" in share_orc:
                time.sleep(2)
                action.click(share_orc["X"][0], share_orc["X"][1])
                print("=================click_ close X")
                time.sleep(3)
                share_orc = ocr()
            if "x" in share_orc:
                time.sleep(2)
                action.click(share_orc["x"][0], share_orc["x"][1])
                print("================click_ close x")
                time.sleep(3)
                share_orc = ocr()
            # share_close_pos = screen.FindImages.find_template([R.img("close_share.png"), ], confidence=0.95)
            time.sleep(1)
            if "退" in share_orc:
                share_close_pos = share_orc["退"]
                action.click(share_close_pos[0], share_close_pos[1])
                print("=================click_ close share")
            time.sleep(2)
            main_txt = ocr()
            time.sleep(1)
            if "猫舍" in main_txt:
                fresh_coin_pos = main_txt["猫舍"]
                action.click(fresh_coin_pos[0], fresh_coin_pos[1])
                print("=================click fresh_coin",fresh_coin_pos)
            time.sleep(3)
            fresh_orc = ocr()
            if "退" in fresh_orc:
                fresh_coin_close = fresh_orc["退"]
                if fresh_coin_close:
                    action.click(fresh_coin_close[0], fresh_coin_close[1])
                    print("=================clic fresh_coin_close")
            #检查是否回到主界面
            for i in range(5):
                main_txt = ocr()
                if "签到" not in main_txt:
                    print("not in main")
                    action.Key.back()
                    time.sleep(1)
                    action.Key.back()
                else:
                    break
                time.sleep(2)
    else:
        action.Key.back()
        time.sleep(1)
        action.Key.back()
# watch_ad_orc()
share_ad_pos= {
    1440:(1320, 2220),
    # 1080:(990,1660)
}

def start(ad_time,fun_list):
    print("================start_watch_ad==========================", )
    display = Device.display()
    # 屏幕宽度
    print(display.widthPixels)
    wid = display.widthPixels
    pro = wid/1440
    share_ad_pos[wid]=(int(share_ad_pos[1440][0]*pro), int(share_ad_pos[1440][1]*pro))
    # 屏幕高度

    print(share_ad_pos)
    while True:
        try:
            if "auto_ad" in fun_list:
                watch_ad_orc(wid)
        except Exception as e:
            print("err =============",e)
        time.sleep(ad_time)
